package com.example.springblog.controller;

import com.example.springblog.Constant.Constant;
import com.example.springblog.model.BlogInfo;
import com.example.springblog.service.BlogService;
import com.example.springblog.utils.JwtUtils;
import io.swagger.models.auth.In;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList(){
        return  blogService.queryBlogList();
    }

    @RequestMapping("/queryBlogDetail")
    public  BlogInfo queryBlogDetail(HttpServletRequest request,
                                     @RequestParam("blogId") Integer blogId){
        log.info("queryBlogDetail,接收参数blogId:"+blogId);
        BlogInfo blogInfo=blogService.queryBlogDetail(blogId);
        //1、获取登录用户信息
        //2、判断登录用户是否为作者
        String user_token  =request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JwtUtils.getUserIdFromToken(user_token);
        if (userId!=null &&userId==blogInfo.getUserId()){
            blogInfo.setLoginUser(true);
        }else{
            blogInfo.setLoginUser(false);
        }
        log.info("queryBlogDetail,接收参数：{}，返回结果{}",blogId,blogInfo);
        return blogInfo;
    }
    @RequestMapping("/add")
    public Boolean publishBlog(HttpServletRequest request,@RequestParam("title") String title, @RequestParam("content") String content){
        log.info("publishBlog,接收参数：title:{}，content:{}",title,content);
        //1、效验参数
        //2、获取当前登录用户
        //3、博客发布
        if (!StringUtils.hasLength(title)||!StringUtils.hasLength(content)){
            log.error("title or contnet 为空");
            return false;
        }
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);  //  user_token从Header拿到
        Integer uderId = JwtUtils.getUserIdFromToken(user_token);   //  从uer_token中解析出userId
        if (uderId == null || uderId<0){
            log.error("用户未登录");
            return  false;
        }
        BlogInfo blogInfo=new BlogInfo(title,content,uderId);
        Integer result = blogService.publishBlog(blogInfo);
        if (result<1){
            log.error("博客发布失败");
            return false;
        }
        return  true;
    }

    /**
     *  编辑 删除
     */
    @RequestMapping("update")
    public Boolean update(Integer blogId,String title,String content){
        log.info("updatBlog，接收参数 title：{}",title,content);
        if (!StringUtils.hasLength(title)||!StringUtils.hasLength(content)){
            log.error("Id、标题、内容为空");
            return false;
        }

        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        blogService.updateBlog(blogInfo);
        return true;
    }

    @RequestMapping("delete")
    public Boolean delete(Integer blogId){
        log.info("deleteBlog,blogId:"+blogId);
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        blogService.updateBlog(blogInfo);
        return true;
    }
}
